package Demo01;
import java.lang.ref.Cleaner;
class Book implements Runnable{
    public Book(){System.out.println("[构造]用心编写了一本优秀的原创技术图书。");
    }
    public void read(){
        System.out.println("[读书]认真学习李兴华老师的编程技术课程。");
    }
    @Override
    public void run(){
        System.out.println("[析构]知识学习完毕，需要努力消化技术概念。");
    }
}
class ThreadCleaner implements AutoCloseable{
    private static final Cleaner cleaner = Cleaner.create();
    private Cleaner.Cleanable cleanable;
    public ThreadCleaner(Book book){
        this.cleanable = cleaner.register(this, book);
    }
    @Override
    public void close() throws Exception{
        this.cleanable.clean();
    }
}
public class ClearDemo01 {
    public static void main (String[] args) throws Exception{
        Book book = new Book();
        try(ThreadCleaner bc = new ThreadCleaner(book)) {
            book.read();
        }catch (Exception e) {}
    }
}
